package qky.api.controller.common;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import qky.api.service.VerifyCodeService;
import qky.common.utils.R;

import java.util.Map;

/**
 * 通用接口:验证码
 */
@RestController
@RequestMapping("/api/cm/code")
@Api(tags="通用接口-验证码", description="【OK】")
public class ApiMobileCheckCodeController {

    @Autowired
    private VerifyCodeService verifyCodeService;
    
    @PostMapping("get")
    @ApiOperation(value = "获取手机号验证码", notes = "mobile:string")
    public R getVerifyCode(@RequestBody JSONObject paramJson){
        verifyCodeService.sendAndCacheVerifyCode(paramJson.getString("mobile"), null);
    	return R.ok();
    }
    
    @PostMapping("check")
    @ApiOperation(value = "核对手机号验证码", notes = "mobile:string,code:string")
    public R checkVerifyCode(@RequestBody JSONObject paramJson){
    	if(!verifyCodeService.checkVerifyCode(paramJson.getString("mobile"), paramJson.getString("code"))) {
    		return R.error("验证码错误或已失效");
    	}
       return R.ok();
    }
    

}
